import { Injectable } from '@angular/core'
import { Validators, FormGroup } from '@angular/forms'
import { alphanumericReg, AcquiringIINMaxLength, IPMaxLength, AbbreviationMaxLength, alphanumericAndSymbolsReg } from 'app/utils/contraints'
import { FieldBase } from './field-base.interface'
import { NumberField } from './field-number.interface'
import { MCCType } from '@shared/components/mcc-select/mcc-select.interface'
import { GroupField } from './field-group.interface'
import { IECUPIChannel } from '@shared/interfaces/acquirer'
import { IECRouteOfStore, IStoreECChanMer } from '@shared/interfaces/store'
import { ChanMerSelectField } from './field-chan-mer-select.interface'
import { TextboxField } from './field-textbox.interface'
import { EPaymentBrand } from '@shared/enums'
import { I18NService } from '@core'

@Injectable()
export class ChannelFormECService {
  constructor(
    private i18n: I18NService
  ) {}

  getUPIChannelFields(val?: IECRouteOfStore): FieldBase<any>[] {
    let selectedChanMer = null
    let cmp: IStoreECChanMer
    if (val && val.chanMerParams) {
      cmp = val.chanMerParams as IStoreECChanMer
      selectedChanMer = {
        id: val.chanMerObjId,
        acquiringIIN: cmp.upiAcquiringIIN,
      }
    }

    const fields: FieldBase<any>[] = [
      new GroupField(
        {
          key: 'chanMerParams',
          order: 1,
        },
        [
          new ChanMerSelectField({
            key: 'selectedChanMer',
            label: 'UPI Acquiring IIN',
            value: selectedChanMer,
            labelKeyOfOption: 'acquiringIIN',
            validators: [Validators.required],
            validatorErrors: {
              required: this.trans('VALIDATION.REQUIRED', { name: 'UPI Acquiring IIN' }),
            },
            options: [],
            compareWith: (o1: any, o2: any) => (o1 && o2 ? o1.acquiringIIN === o2.acquiringIIN : o1 === o2),
            disabled: !!(val && val.id),
          }),
        ], ),
          new TextboxField({
            key: 'upiMerchantID',
            label: 'UPI Merchant ID',
            validators: [Validators.required, Validators.maxLength(IPMaxLength), Validators.pattern(alphanumericReg)],
            validatorErrors: {
              required: this.trans('VALIDATION.REQUIRED', { name: 'UPI Merchant ID' }),
              maxlength: this.trans('VALIDATION.MAX_LENGTH', { length: IPMaxLength }),
              pattern: this.trans('VALIDATION.PATTERN.ALPHANUMERIC_ONLY'),
            },
            value: (cmp && cmp.upiMerchantID) || undefined,
            order: 2,
          }),
          new TextboxField({
            key: 'upiMerchantAbbreviation',
            label: 'Merchant Abbreviation',
            validators: [
              Validators.required,
              Validators.maxLength(AbbreviationMaxLength),
              Validators.pattern(alphanumericAndSymbolsReg),
            ],
            validatorErrors: {
              required: this.trans('VALIDATION.REQUIRED', { name: 'Merchant Abbreviation' }),
              maxlength: this.trans('VALIDATION.MAX_LENGTH', { length: AbbreviationMaxLength }),
              pattern: this.trans('VALIDATION.PATTERN.ALPHANUMERIC_SYMBOLS_ONLY'),
            },
            value: (cmp && cmp.upiMerchantAbbreviation) || undefined,
            mccType: MCCType.Standard,
            order: 3,
          }),
          new NumberField({
            key: 'fee',
            label: 'Transaction Fee',
            validators: [Validators.required],
            validatorErrors: {
              required: this.trans('VALIDATION.REQUIRED', { name: 'Transaction Fee' }),
            },
            value: (cmp && cmp.fee) || undefined,
            order: 4,
          }),
    ]
    return fields
  }

  private trans(str: string, params?: Object): string {
    return this.i18n.fanyi(str, params)
  }
}
